import cfg from '@/common/config.js'
import { msg, nav, storage, timestamp } from './fun';

const res = {
    ok: 0,
    msg: '异常错误'
};

function f_token() {
    return cfg.token()
}
// console.log(f_token());
export function req(url, data, type, loading) {
    return new Promise((resolve, reject) => {
        let token = cfg.token()
        type = type || 'form';
        let method = type == 'form' ? 'GET' : 'POST';
        let types = type == 'form' ? 'application/x-www-form-urlencoded' : 'application/json';
        if (token) {
            var herder = { 'Content-Type': types, 'token': token };

        } else {
            var herder = { 'Content-Type': types };
        }
        var urls = url; if (url.substr(0, 4) != 'http') urls = cfg.baseurl + url;
        loading = loading == undefined ? true : loading;
        loading && uni.showLoading({ mask: true });
        uni.request({
            header: herder,
            url: urls,
            method,
            data: data,
            dataType: 'json',
        }).then(response => {
            console.log(response);
            if (response[1].data.code == 100) {
                uni.removeStorage({
                    key: cfg.sys + '_token',
                })
                nav('/pages/login/login', 'm')
            } else {
                loading && uni.hideLoading();
                resolve(response[1].data)
            }

        }).catch(err => {
            loading && uni.hideLoading();
            resolve(res)
        })
    });
}

export function req_post(url, data, type, loading) {
    return new Promise((resolve, reject) => {
        let token = cfg.token()
        type = type || 'form';
        type = type == 'form' ? 'application/x-www-form-urlencoded' : 'application/json';
        if (token) {
            var herder = { 'Content-Type': type, 'token': token };
        } else {
            var herder = { 'Content-Type': type };
        }
        var urls = url; if (url.substr(0, 4) != 'http') urls = cfg.baseurl + url;
        loading = loading == undefined ? true : loading;
        loading && uni.showLoading({ mask: true });
        uni.request({
            header: herder,
            url: urls,
            method: 'POST',
            data: data,
            dataType: 'json',
        }).then(response => {
            if (response[1].data.code == 100) {
                cfg.flag(true);
                uni.removeStorage({
                    key: cfg.sys + '_token',
                })
                if (cfg.flag()) {
                    // console.log(cfg.flag());
                    // #ifdef H5
                    nav('/pages/login/login', 'm')
                    // #endif	
                    // #ifdef MP-WEIXIN
                    nav('/pages/AuthorizedLogin/AuthorizedLogin', 'm')
                    // #endif	
                    // #ifdef APP-PLUS
                    nav('/pages/login/login', 'm')
                    // #endif	
                }
            } else {
                loading && uni.hideLoading();
                resolve(response[1].data)
            }
        }).catch(err => {
            console.log('666');
            loading && uni.hideLoading();
            resolve(res)

        })
    });
}



export function req_common(url, data, type, loading) {
    return new Promise((resolve, reject) => {
        let token = cfg.token()
        let method = type == 'form' ? 'GET' : 'POST';
        type = type || 'form';
        type = type == 'form' ? 'application/x-www-form-urlencoded' : 'application/json';
        if (token) {
            var herder = { 'Content-Type': type, 'token': token };
        } else {
            var herder = { 'Content-Type': type };
        }
        var urls = url; if (url.substr(0, 4) != 'http') urls = cfg.commonurl + url;
        loading = loading == undefined ? true : loading;
        loading && uni.showLoading({ mask: true });
        uni.request({
            header: herder,
            url: urls,
            method,
            data: data,
            dataType: 'json',
        }).then(response => {
            console.log(response);
            if (response[1].data.code == 100) {
                // #ifdef H5
                nav('/pages/login/login', 'm')
                // #endif	
                // #ifdef MP-WEIXIN
                nav('/pages/AuthorizedLogin/AuthorizedLogin', 'm')
                // #endif	
                // #ifdef APP-PLUS
                nav('/pages/login/login', 'm')
                // #endif	
            } else {
                loading && uni.hideLoading();
                resolve(response[1].data)
            }
        }).catch(err => {
            loading && uni.hideLoading();
            resolve(res)

        })
    });
}


